Each problem must be submitted as its own .cpp file. Grading for each problem is as follows:
Functions
- Write a complete C++ function that does the following:
- Prompts the user for a positive integer with 5 or more digits.
- Forces the user to enter a valid input.
- Includes a function called sumGaps that does the following:
- Computes the arithmetic difference ("gap") between adjacent digits of the input number.
- The "gap" is always the absolute value of this number.
- Returns the sum of the gaps between the adjacent digits.
- In the main function, prints the original value from the user, the gap sum, and the result when the gap sum is subtracted from the user's number .
Sample run of program:
Enter a number with five or more digits: 25361
Your original number is: 25361
The gap sum is 13
The original number minus the gap sum is 25348
- Write a complete C++ program that does the following:
- Prompts the user for a number n between 4 and 16.
- If the user does not enter a valid input, give them two more tries (for a total of three) to enter a valid input.
- If the user does not enter a valid input, sets n equal to 10.
- Asks the user if they would like to print a triangle or a square and store the input in variable shape
- Your program must include a function called printTriangle and another function called printSquare. Each function takes n as input. Your program should then do the following;
- If the user entered "triangle", call printTriangle which prints a right triangle with n rows and n columns.
- If the user entered "square", call printSquare which prints a square with n rows and n columns.
- If the user enters something other than "triangle" or "square" your program can do whatever you want (for example, just end, print a message to a user, choose the shape to print, etc.)
Sample run of program:
Enter a number between 4 and 16: 3
Bad input! Try again: 2
Bad input! Try again: 1
Would you like a triangle or a square? triangle
*
**
***
****
*****
******
*******
********
*********
**********
Another sample run of program:
Enter a number between 4 and 16: 5
Would you like a triangle or a square? square
*****
*****
*****
*****
*****
- Write a complete C++ program that does the following:
- Prompts the user for a number with at least three digits.
- If the user enters an invalid number, say "Goodbye" and end the program.
- Includes a function called parityCheck that takes as input the user's number and does the following:
- If the digits of the user's number are all odd, prints "All odd."
- If the digits of the user's number are all even, prints "All even."
- If the digits of the user's number are not all odd or all even, prints "Mixed parity."
Sample run of program:
Enter a number with at least three digits: 1233
Mixed parity
Another sample run of program:
Enter a number with at least three digits: 22480
All even
Another sample run of program:
Enter a number with at least three digits: 333
All odd
Reference Parameters
- Write a complete C++ program that does the following:
- Prompts the user for three integers and read the values into variables a, b and c.
- Asks the user if they would like the numbers sorted in ascending or descending order.
- Write a function called updateAscending that does the following:
- Has three integer reference parameters a, b and c.
- The function updates the variable assignments so that a stores the largest number, b stores the middle number, and c stores the smallest number.
- Write a function called updateDescending that does the following:
- Has three integer reference parameters a, b and c.
- The function updates the variable assignments so that a stores the smallest number, b stores the middle number, and c stores the largest number.
- In main, if the user chose ascending order, pass the variables to updateAscending. If the user chose descending order, pass the variables to updateDescending.
- After the function call, prints the values stored in a, b and c in main to confirm they are sorted correctly.
Sample run of program:
Enter three numbers: 1 3 2
Would you like the numbers sorted in ascending or descending order? ascending
Your sorted variables now store: a = 1, b = 2, c = 3
Another sample run of program:
Enter three numbers: 7 2 4
Would you like the numbers sorted in ascending or descending order? descending
Your sorted variables now store: a = 7, b = 4, c = 2
- Write a complete C++ program that does the following:
- Prompts the user for three scores, each out of 100.
- Computes the curve by calculating the difference between 100 and the largest score.
- Write a function applyCurve that does the following:
- Takes as input three integer reference parameters (scores) and an integer curve.
- Update the scores in the function by adding the curve to them.
- In main, passes the three scores and curve to applyCurve.
- Prints the curve value and the curved scores.
Sample run of program:
Enter three scores: 87 89 82
The curve is 11.
The scores after applying the curve are 98, 100, 93.
Recursion
- Write a complete C++ function that does the following:
- Prompts the user for a number greater than or equal to 100.
- Forces the user to enter a valid input.
- Write a recursive function called getFirstThreeSum that does the following:
- Takes an integer parameter.
- Computes the sum of the leftmost three digits of the input.
- Returns this sum to the calling function.
- Write a function called printRange that does the following:
- Takes an integer parameter.
- If the integer is even, the function prints all even numbers from 0 through the number passed as input to the printRange function.
- If the integer is odd, the function prints all odd numbers from 1 through the number passed as input to the printRange function.
- The main function passes the user's input to getFirstThreeSum and prints the result to the monitor.
- The main function then takes the return value from getFirstThreeSum and passes it as the input to the printRange function.
Sample run of program:
Enter a number greater than or equal to 100: 2336
The sum of the first three digits is 8
0 2 4 6 8
Another sample run of program:
Enter a number greater than or equal to 100: 19374
The sum of the first three digits is 13
1 3 5 7 9 11 13
- Generates the first five to twenty numbers in this sequence: a_0 = 0 (base case); a_{n} = 2*a_{n-1} + 3 (recursive definition).
- Hint: Look up recursive implementations of the Fibonacci sequence online.
- Write a recursive function called recursiveSequence that takes an integer parameter.
- This recursive function generates the ith number in the defined sequence.
- It needs two lines of code:
- The base case.
- The recursive call.
- Your main function should prompt the user for a number n between 5 and 20.
- If the user enters an invalid input, say "Goodbye" and end the program.
- If the input is valid, your program should print the first n numbers in the recursive sequence defined above.
Sample run of program:
Enter a number between 5 and 20: 7
0 3 9 21 45 93 189
Another sample run of program:
Enter a number between 5 and 20: 12
0 3 9 21 45 93 189 381 765 1533 3069 6141
- Write a complete C++ function that does the following:
- Prompts the user for a positive integer greater than or equal to 3.
- Force the user to enter a valid input.
- Write a recursive function called changeThrees that does the following:
- Modifies the input value by changing any digit equal to 3 to a 2.
- Prints "removed a 3" every time a 3 is changed to a 2.
- Returns the modified number to the calling function.
- Your main function should print the number returned by the changeThrees function, then print "I don't like numbers with 3's in them."
Sample run of program:
Enter a number greater than or equal to 3: 23353
removed a 3
removed a 3
removed a 3
22252
I don't like numbers with 3's in them.
Another sample run of program:
Enter a number greater than or equal to 3: 5764
5764
I don't like numbers with 3's in them.